package com.manager.commons.utils;

//                   _ooOoo_    
//                  o8888888o    
//                  88" . "88    
//                  (| -_- |)    
//                   O\ = /O    
//               ____/'---'\____    
//              /. '\\| : |//' .\    
//             // \\||| : |||// \\   
//            // _|||||-:-|||||_ \\    
//           // | | \\\ - /// | | \\
//           || |_| ''\---/'' |_| || 
//            \ .-\___/'-'\___/-. /    
//          ___\. .' /--.--\ '. ./___    
//        .""'< `.___\_<|>_/___.' >'"".    
//       | |: `- \`.|'\ _ /'|.`/ -` :| |    
//        \ \ `-. \_ __\ /__ _/ .-` / /    
//======'-.___`-.___\_______/___.-`___.-'======    
//                   '=---='    
//.................................................    
//写一哈代码，你就青叫唤；信龟儿设计，你逗是傻逼。
//永无Bug，啷个可能！求佛主保佑也没球用！

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * DateUtil(时间)
 *
 * @author H
 * @version V1.0
 */
public class DateUtil {

    public static final String BASE_DATE = "2021-01-01 00:00:00";

    /*** 基础时间 取 2000-01-03 00:00:00 星期1 */
    public static final long DATE_BASE = 946828800000L;

    public static final long DATE_MINUTE = 1000 * 60;
    public static final long DATE_HOUR = DATE_MINUTE * 60;
    public static final long DATE_DATE = DATE_HOUR * 24;
    public static final long DATE_WEEK = DATE_DATE * 7;
    public static final long DATE_MONTH = DATE_DATE * 30;

    /*** 时间格式化字符串(yyyy-MM-dd) */
    public static final String DATE_FORMAR_STRING = "yyyy-MM-dd";
    /**
     * 时间格式化字符串(HH:mm:ss)
     */
    private static final String TIME_FORMAR_STRING = "HH:mm:ss";
    /*** 时间格式化字符串(yyyy-MM-dd HH:mm:ss) */
    public static final String DATETIME_FORMAR_STRING = "yyyy-MM-dd HH:mm:ss";

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAR_STRING);
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAR_STRING);
    private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat(DATETIME_FORMAR_STRING);
    public static final String DATE_FORMAR_STRING_yyyyMMdd = "yyyyMMdd";
    public static final String DATETIME_FORMAR_STRING_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAR_STRING_yyyyMMddHHmm = "yyyyMMddHHmm";


    /**
     * 获取当前时间(yyyy-MM-dd HH:mm:ss)
     *
     * @return
     */
    public static String currentDateTime() {
        return datetimeFormat.format(new Date());
    }

    /**
     * 获取当前时间(yyyy-MM-dd)
     *
     * @return
     */
    public static String currentDate() {
        return dateFormat.format(new Date());
    }

    public static String currentDate(String format) {
        SimpleDateFormat dateFormat_ = new SimpleDateFormat(format);
        return dateFormat_.format(new Date());
    }

    public static String currentDateThirty() {
        LocalDate today = LocalDate.now(); // 获取当前日期
        LocalDate thirtyDaysBefore = today.minusDays(30); // 获取30天前的日期

        // 如果需要以特定格式输出日期，可以使用DateTimeFormatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = thirtyDaysBefore.format(formatter);

        System.out.println("30天前的日期是: " + formattedDate);
        return formattedDate;
    }

    /**
     * 获取yyyy,mm,dd(0:year;1:MM;2:dd,3:yyyy-MM-dd)
     *
     * @param index
     * @return
     */
    public static String getYmd(int index) {
        String[] ymds = getYmds(1);
        if (index >= 0 || index <= 2) {
            return ymds[index];
        } else {
            return StringUtil.join(ymds, "-");
        }
    }

    /**
     * 获取yyyy,mm,dd
     *
     * @return
     */
    public static String[] getYmds(int zero) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int date = calendar.get(Calendar.DATE);
        String yyyy = String.valueOf(year);
        String mm = (zero > 0 ? ((month < 10 ? "0" : "") + month) : month + "");
        String dd = (zero > 0 ? ((date < 10 ? "0" : "") + date) : date + "");
        return new String[]{yyyy, mm, dd};
    }

    /**
     * 时间格式化
     *
     * @param date
     * @param separator -/
     * @return yyyy-MM-dd yyyy/MM/dd
     */
    public static String formatDate(Date date, String separator) {
        if (null == date) {
            return "";
        } else {
            if (null == separator) {
                return dateFormat.format(date);
            } else {
                String format = StringUtil.join(new String[]{"yyyy", "MM", "dd"}, separator);
                return new SimpleDateFormat(format).format(date);
            }
        }
    }

    /**
     * 时间格式化
     *
     * @param date
     * @param separator :
     * @return HH:mm:ss HHmmss
     */
    public static String formatTime(Date date, String separator) {
        if (null == date) {
            return "";
        } else {
            if (null == separator) {
                return timeFormat.format(date);
            } else {
                String format = StringUtil.join(new String[]{"HH", "mm", "ss"}, separator);
                return new SimpleDateFormat(format).format(date);
            }
        }
    }

    /**
     * 时间格式：字符串(yyyy-MM-dd HH:mm:ss)
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String formatDateTime(Date date) {
        if (null == date) {
            return "";
        } else {
            return datetimeFormat.format(date);
        }
    }

    /**
     * 时间格式：字符串
     *
     * @param format
     * @return
     * @throws ParseException
     */
    public static String formatTTime(String format) throws Exception {
        Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").parse(format);
        Date date1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK).parse(date.toString());
        return datetimeFormat.format(date1);
    }

    /**
     * 时间格式：字符串（format）
     *
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            return new SimpleDateFormat(format).format(date);
        }
    }

    /**
     * 时间格式：Date(yyyy-MM-dd)
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String date) throws ParseException {
        if (StringUtil.isEmpty(date)) {
            return new Date();
        }
        return dateFormat.parse(date);
    }

    /**
     * 时间格式：Date（HH:mm:ss）
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date parseTime(String date) throws ParseException {
        if (StringUtil.isEmpty(date)) {
            return new Date();
        }
        return timeFormat.parse(date);
    }

    /**
     * 时间格式：Date（yyyy-MM-dd HH:mm:ss）
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date parseDateTime(String date) throws ParseException {
        if (StringUtil.isEmpty(date)) {
            return new Date();
        }
        return datetimeFormat.parse(date);
    }

    /**
     * 时间格式：Date（date,format）
     *
     * @param date
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date parse(String date, String format) throws ParseException {
        if (StringUtil.isEmpty(date)) {
            return new Date();
        }
        return new SimpleDateFormat(format).parse(date);
    }

    /**
     * 通过传入时间与当前时间比较，获得时间差值形成文字
     *
     * @param date 传入时间
     * @return 串
     */
    public static String getStringForDate(Date date) {
        String result = "";
        Calendar input = Calendar.getInstance();
        input.setTime(date);
        Calendar now = Calendar.getInstance();
        if (now.get(Calendar.YEAR) == input.get(Calendar.YEAR)
                && now.get(Calendar.MONTH) == input.get(Calendar.MONTH)) {
            int day = now.get(Calendar.DATE) - input.get(Calendar.DATE);
            switch (day) {
                case 0:
                    break;
                case 1:
                    result = "昨天";
                    break;
                default:
                    result = format(input.getTime(), "yyyy年MM月dd日");
                    break;
            }
            result = result + format(input.getTime(), "HH:mm");
        } else {
            result = format(input.getTime(), "yyyy年MM月dd日HH:mm");
        }
        return result;
    }

    /**
     * 通过传入时间与当前时间比较，获得时间差值形成文字(简易)
     *
     * @param date 传入时间
     * @return 串
     */
    public static String getSimpleStringForDate(Date date) {
        String result = "";
        Calendar input = Calendar.getInstance();
        input.setTime(date);
        Calendar now = Calendar.getInstance();
        int day = now.get(Calendar.DATE) - input.get(Calendar.DATE);
        switch (day) {
            case 0:
                result = format(input.getTime(), "HH:mm");
                break;
            default:
                result = format(input.getTime(), "yyyy-MM-dd");
                break;
        }
        return result;
    }

    /**
     * 判定2个时间
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int compareDate(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            return 1;
        } else if (date1.getTime() < date2.getTime()) {
            return -1;
        } else {
            return 0;
        }
    }

    /**
     * 判定2个时间
     *
     * @param str1
     * @param str2
     * @return
     * @throws ParseException
     */
    public static int compareDate(String str1, String str2) throws ParseException {
        Date date1 = ((StringUtil.isEmpty(str1)) ? new Date() : parseDate(str1));
        Date date2 = parseDate(str2);
        return compareDate(date1, date2);
    }

    /**
     * 时间差
     *
     * @param str1 str1为null则自动取当前时间
     * @param str2
     * @return
     * @throws ParseException
     */
    public static long calcTimeMillis(String str1, String str2) throws ParseException {
        Calendar date1 = Calendar.getInstance();
        if (StringUtil.isNotEmpty(str1)) {
            if (str1.length() > 10) {
                date1.setTime(parseDateTime(str1));
            } else {
                String[] times = str1.split("-");
                date1.set(Integer.valueOf(times[0]), Integer.valueOf(times[1]) - 1, Integer.valueOf(times[2]));
            }
        }
        Calendar date2 = Calendar.getInstance();
        if (str2.length() > 10) {
            date2.setTime(parseDateTime(str2));
        } else {
            String[] times = str2.split("-");
            date2.set(Integer.valueOf(times[0]), Integer.valueOf(times[1]) - 1, Integer.valueOf(times[2]));
        }
        long t = date1.getTimeInMillis() - date2.getTimeInMillis();
        return t;
    }

    /**
     * 计算分钟数
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static long calcMinute(String date1, String date2) throws ParseException {
        long minute = (calcTimeMillis(date1, date2) / DATE_MINUTE);
        return minute;
    }

    /**
     * 计算天数(如果date1为null则取当前时间)
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static long calcDate(String date1, String date2) throws ParseException {
        long date = (calcTimeMillis(date1, date2) / DATE_DATE);
        return date;
    }

    /**
     * 时间差
     *
     * @param date1 date1为null则取当前时间
     * @param date2
     * @return
     * @throws ParseException
     */
    public static long calcTimeMillis(Date date1, Date date2) throws ParseException {
        Calendar a = Calendar.getInstance();
        if (null != date1) {
            a.setTime(date1);
        }
        Calendar b = Calendar.getInstance();
        b.setTime(date2);
        return (a.getTimeInMillis() - b.getTimeInMillis());
    }

    /**
     * 计算分钟数
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static long calcMinute(Date date1, Date date2) throws ParseException {
        return (calcTimeMillis(date1, date2) / DATE_MINUTE);
    }

    /**
     * 计算天数(如果date1为null则取当前时间)
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static long calcDate(Date date1, Date date2) throws ParseException {
        return (calcTimeMillis(date1, date2) / DATE_DATE);
    }

    /**
     * 获得相差月数
     *
     * @param date1
     * @param date2
     * @return 相差月数 如果传入时间大于当前系统时间为负数
     * @date 2011-05-18
     */
    public static long calcMonth(Date date1, Date date2) {
        Calendar a = Calendar.getInstance();
        if (null != date1) {
            a.setTime(date1);
        }
        Calendar b = Calendar.getInstance();
        b.setTime(date2);
        int year1 = a.get(Calendar.YEAR), year2 = b.get(Calendar.YEAR);
        int month1 = a.get(Calendar.MONTH), month2 = b.get(Calendar.MONTH);
        return year1 * 12L + month1 - year2 * 12L - month2;
    }

    /**
     * 获得时间差
     *
     * @param date1 date1<=0则自动取当前时间
     * @param date2
     * @return
     */
    public static long calcTimeMillis(long date1, long date2) {
        long startTime = (date1 > 0 ? date1 : System.currentTimeMillis()) - DATE_BASE;
        long endTime = date2 - DATE_BASE;
        return startTime - endTime;
    }

    /**
     * 计算天数(如果date1<=0则取当前时间)
     *
     * @param date1 date1<=0则取当前时间
     * @param date2
     * @return
     */
    public static long calcDate(long date1, long date2) {
        return (calcTimeMillis(date1, date2) / DATE_DATE);
    }

    /**
     * 计算天数(如果date1<=0则取当前时间)
     *
     * @param date1 date1<=0则取当前时间
     * @param date2
     * @return
     */
    public static long calcMinute(long date1, long date2) {
        return (calcTimeMillis(date1, date2) / DATE_MINUTE);
    }

    /**
     * 获得相差周数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long calcWeek(long date1, long date2) {
        return (calcTimeMillis(date1, date2) / DATE_WEEK);
    }

    /**
     * 获得相差月数
     *
     * @param date1
     * @param date2
     * @return 相差月数 如果传入时间大于当前系统时间为负数
     * @date 2011-05-18
     */
    public static long calcMonth(long date1, long date2) {
        Calendar now = Calendar.getInstance();
        if (date1 > 0) {
            now.setTimeInMillis(date1);
        }
        Calendar end = Calendar.getInstance();
        end.setTimeInMillis(date2);
        int year1 = now.get(Calendar.YEAR);
        int month1 = now.get(Calendar.MONTH);
        int year2 = end.get(Calendar.YEAR);
        int month2 = end.get(Calendar.MONTH);
        return year1 * 12L + month1 - year2 * 12L - month2;
    }

    /**
     * 计算年龄
     *
     * @param date 出生日期
     * @return
     */
    public static int calcAge(Date date) {
        if (date == null) {
            return 0;
        }
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        now.setTime(date);
        return year - now.get(Calendar.YEAR);
    }

    /**
     * 计算年龄
     *
     * @param date 出生日期
     * @return
     */
    public static int calcAge(String date) {
        if (date == null) {
            return 0;
        }

        try {
            Calendar now = Calendar.getInstance();
            int year = now.get(Calendar.YEAR);
            now.setTime(parseTime(date));
            return year - now.get(Calendar.YEAR);
        } catch (ParseException e) {
            return 0;
        }

    }

    /**
     * 根据生日计算他的年龄友好现实
     *
     * @param date
     * @return
     */
    public static String getDateAge(String date) {
        LocalDate birthDate = LocalDate.parse(date); // 示例生日日期
        LocalDate currentDate = LocalDate.now(); // 当前日期

        Period age = Period.between(birthDate, currentDate);
        StringBuffer sb = new StringBuffer();
        if (age.getYears() > 0) {
            sb.append(age.getYears()).append("岁");
        }
        if (age.getMonths() > 0) {
            sb.append(age.getMonths()).append("个月");
        }
        sb.append(age.getDays()).append("天");
        return sb.toString();
    }

    /**
     * 根据传入时间 获得本周第一天（周一)的时间
     *
     * @param date
     * @return 返回时间 时分秒毫秒为0
     */
    public static long getFristDayForWeek(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        int tempday = calendar.get(Calendar.DAY_OF_WEEK);
        switch (tempday) {
            case 1:
                // 星期天
                calendar.add(Calendar.DATE, -6);
                break;
            default:
                // 周一到周六
                calendar.add(Calendar.DATE, -(tempday - 2));
                break;
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime().getTime();
    }

    /**
     * 根据传入时间 获得本周第一天（周一)的时间
     *
     * @return 返回时间 时分秒毫秒为0
     */
    public static long getFristDayForWeek() {
        return getFristDayForWeek(System.currentTimeMillis());
    }

    /**
     * 根据传入时间 获得本周最后一天（周日)的时间
     *
     * @param date
     * @return 返回时间 时分秒毫秒为23:59:59 999
     */
    public static long getLastDayForWeek(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        int tempday = calendar.get(Calendar.DAY_OF_WEEK);
        switch (tempday) {
            case 1:
                // 星期天
                break;
            default:
                // 周一到周六
                calendar.add(Calendar.DATE, 8 - tempday);
                break;
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime().getTime();
    }

    /**
     * 根据传入时间 获得本周最后一天（周日)的时间
     *
     * @return 返回时间 时分秒毫秒为23:59:59 999
     */
    public static long getLastDayForWeek() {
        return getLastDayForWeek(System.currentTimeMillis());
    }

    /**
     * 根据时间所在月的第一天的日期
     *
     * @param date
     * @return
     */
    public static Date getMonthFristDay(long date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR));
        cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
        return cal.getTime();
    }

    /**
     * 根据时间所在月的最后一天的日期
     *
     * @param date
     * @return
     */
    public static Date getMonthLastDay(long date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR, cal.getActualMaximum(Calendar.HOUR));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
        return cal.getTime();
    }

    /**
     * 根据时间所在年的第一天的日期
     *
     * @param date
     * @return
     */
    public static Date getYearFristDay(long date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date);
        cal.set(Calendar.MONTH, cal.getActualMinimum(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR));
        cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
        return cal.getTime();
    }

    /**
     * 根据时间所在年的最后一天的日期
     *
     * @param date
     * @return
     */
    public static Date getYearLastDay(long date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date);
        cal.set(Calendar.MONTH, cal.getActualMaximum(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR, cal.getActualMaximum(Calendar.HOUR));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
        return cal.getTime();
    }

    /**
     * 获得本周第一天（周一）的时间
     *
     * @param date
     * @return 返回时间 时分秒毫秒为00:00:00 000
     */
    public static Date getFristDayForWeekDate(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        int tempday = calendar.get(Calendar.DAY_OF_WEEK);
        switch (tempday) {
            case 1:
                // 星期天
                calendar.add(Calendar.DATE, -6);
                break;
            default:
                // 周一到周六
                calendar.add(Calendar.DATE, -(tempday - 2));
                break;
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得本周最后一天（周日）的时间
     *
     * @param date
     * @return 返回时间 时分秒毫秒为23:59:59 999
     */
    public static Date getLastDayForWeekDate(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        int tempday = calendar.get(Calendar.DAY_OF_WEEK);
        switch (tempday) {
            case 1:
                // 星期天
                break;
            default:
                // 周一到周六
                calendar.add(Calendar.DATE, 8 - tempday);
                break;
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 当前时间开始（如2013-10-01 00:00:00）
     *
     * @param date
     * @return
     */
    public static Date getCurrentDayBeginDate(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 当前时间结束（如2013-10-01 23:59:59）
     *
     * @param date
     * @return
     */
    public static Date getCurrentDayEndDate(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 是否今天
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static boolean isToday(String time) throws ParseException {
        Calendar input = Calendar.getInstance();
        input.setTime(parseDateTime(time));
        Calendar now = Calendar.getInstance();
        if (now.get(Calendar.YEAR) == input.get(Calendar.YEAR)
                && now.get(Calendar.MONTH) == input.get(Calendar.MONTH)) {
            return now.get(Calendar.DATE) - input.get(Calendar.DATE) == 0;
        }
        return false;
    }

    /**
     * 获取日期是第几周
     *
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(7);
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取时间是星期几
     *
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date, int year) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week = cal.get(Calendar.DAY_OF_WEEK);
        if (year > 0) {
            week = cal.get(Calendar.WEEK_OF_YEAR);
        }
        return week;
    }

    /**
     * 获取时间是星期几
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getDayOfWeek(String date) throws ParseException {
        String[] weeks = {"日", "一", "二", "三", "四", "五", "六"};
        Date d = ((null == date ? new Date() : parseDate(date)));
        int w = (getDayOfWeek(d, -1) - 1);
        if (w < 0)
            w = 0;
        return weeks[w];
    }

    /**
     * 获取两个时间段所有日期
     *
     * @param begin
     * @param end
     * @return
     */
    public static List<Date> getDates(Date begin, Date end) {
        List<Date> dates = new ArrayList<Date>();
        dates.add(begin);
        Calendar date1 = Calendar.getInstance();
        date1.setTime(begin);
        Calendar date2 = Calendar.getInstance();
        date2.setTime(end);
        while (date2.after(date1.getTime())) {
            date1.add(Calendar.DAY_OF_MONTH, 1);
            dates.add(date1.getTime());
        }
        return dates;
    }

    /**
     * 获取两个时间段所有日期
     *
     * @param begin
     * @param end
     * @return
     * @throws Exception
     */
    public static List<Date> getDates(String begin, String end) throws Exception {
        Date date1 = parseDate(begin);
        Date date2 = parseDate(end);
        return getDates(date1, date2);
    }

    /**
     * 获取两个时间段所有日期
     *
     * @param begin
     * @param end
     * @return
     */
    public static List<String> getDates(Date begin, Date end, String formatStr, Boolean weekend) {
        List<String> dates = new ArrayList<String>();
        dates.add(format(begin, formatStr));
        Calendar date1 = Calendar.getInstance();
        date1.setTime(begin);
        Calendar date2 = Calendar.getInstance();
        date2.setTime(end);
        boolean flag = weekend;
        while (date2.getTime().after(date1.getTime())) {
            date1.add(Calendar.DAY_OF_MONTH, 1);
            if (weekend) {
                // 判断日期是否是周六周日
                flag = date1.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY
                        && date1.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY;
            } else {
                flag = true;
            }
            if (flag) {
                dates.add(format(date1.getTime(), formatStr));
            }
        }
        return dates;
    }

    public static ArrayList<String> getDatesBetween(String startDateStr, String endDateStr) {
        ArrayList<String> dateList = new ArrayList<>();
        if (StringUtil.isNotEmpty(startDateStr) && StringUtil.isNotEmpty(endDateStr)) {
            try {
                Date startDate = dateFormat.parse(startDateStr);
                Date endDate = dateFormat.parse(endDateStr);
                Calendar startCal = Calendar.getInstance();
                startCal.setTime(startDate);
                Calendar endCal = Calendar.getInstance();
                endCal.setTime(endDate);
                // 确保结束日期在日期列表中
                endCal.add(Calendar.DATE, 1);
                while (startCal.before(endCal)) {
                    dateList.add(dateFormat.format(startCal.getTime()));
                    startCal.add(Calendar.DAY_OF_YEAR, 1);
                }
            } catch (Exception e) {

            }
        }
        return dateList;
    }

    public static Map<String, String> getDatesBetweenMap(String startDateStr, String endDateStr) {
        Map<String, String> dateList = new HashMap<>();
        if (StringUtil.isNotEmpty(startDateStr) && StringUtil.isNotEmpty(endDateStr)) {
            try {
                Date startDate = dateFormat.parse(startDateStr);
                Date endDate = dateFormat.parse(endDateStr);
                Calendar startCal = Calendar.getInstance();
                startCal.setTime(startDate);
                Calendar endCal = Calendar.getInstance();
                endCal.setTime(endDate);
                // 确保结束日期在日期列表中
                endCal.add(Calendar.DATE, 1);
                while (startCal.before(endCal)) {
                    String d = dateFormat.format(startCal.getTime());
                    dateList.put(d, d);
                    startCal.add(Calendar.DAY_OF_YEAR, 1);
                }
            } catch (Exception e) {

            }
        }
        return dateList;
    }

    /**
     * 获取两个时间段所有日期
     *
     * @param begin
     * @param end
     * @return
     * @throws Exception
     */
    public static List<String> getDates(String begin, String end, String format, Boolean weekend) throws Exception {
        Date date1 = parseDate(begin);
        Date date2 = parseDate(end);
        return getDates(date1, date2, format, weekend);
    }

    /**
     * 更具当前时间计算年龄
     *
     * @param birthdayStr
     * @return
     */
    public static int calculateAge(String birthdayStr) {
        // 假设生日字符串格式是"YYYY-MM-DD"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate birthday = LocalDate.parse(birthdayStr, formatter);
        LocalDate today = LocalDate.now();

        int age = today.getYear() - birthday.getYear();
        if (today.isBefore(birthday.plusYears(age))) {
            // 如果今天还没过今年的生日，那么年龄减一
            age--;
        }
        return age;
    }

    /**
     * 上午还是下午
     *
     * @param date
     * @return
     */
    public static String morningOrAfternoon(String date) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(parseDateTime(date));
            // 获取小时数（24小时制）
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if (hour >= 6 && hour < 12) {
                return "上午";
            } else {
                return "下午";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "未知";
    }

    /**
     * 上午还是下午
     *
     * @param date
     * @return
     */
    public static String morningOrAfternoon2(String date) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(parseDateTime(date));
            // 获取小时数（24小时制）
            String _date = date.substring(0, 11);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if (hour >= 6 && hour < 12) {
                return _date + "上午";
            } else {
                return _date + "下午";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "未知";
    }

    /**
     * 时间格式：字符串（format）
     *
     * @param str
     * @return
     */
    public static String formatChinese(String str) {
        // 将输入字符串按格式解析为LocalDate对象
        LocalDate date = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 将LocalDate对象按指定格式格式化为字符串
        String output = date.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        return output;
    }

    /**
     * 时间格式：字符串（format）
     *
     * @param str
     * @return
     */
    public static String formatMMdd(String str) {
        // 将输入字符串按格式解析为LocalDate对象
        LocalDate date = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 将LocalDate对象按指定格式格式化为字符串
        String output = date.format(DateTimeFormatter.ofPattern("MM-dd"));
        return output;
    }
}